﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Acrobat;

namespace TMS
{
    public partial class ProductsControl : UserControl
    {
        private bool listOfProdutsLoaded = false;
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();
        Categories cat = new Categories();
        Product prod = new Product();
        DataSet table1 = new DataSet();
        Users ActiveUser = Program.GetActiveUser;

        public ProductsControl()
        {
            Program.GetMainForm.Text = "TMS - Производи";
            InitializeComponent();
            if (!listOfProdutsLoaded)
            {
                loadListOfProduts();
                listOfProdutsLoaded = true;
            }
        }

        public ProductsControl(DataGridViewRow parameters)
        {
            Program.GetMainForm.Text = "TMS - Производи:";
            InitializeComponent();
            if (!listOfProdutsLoaded)
            {
                loadListOfProduts();
                listOfProdutsLoaded = true;
            }
            cmbCategory1.SelectedValue = parameters.Cells[0].Value.ToString();
            cmbCategory2.SelectedValue = parameters.Cells[1].Value.ToString();
            cmbCategory3.SelectedValue = parameters.Cells[2].Value.ToString();
        }

        private void loadListOfProduts()
        {
            cmbCategory1.DisplayMember = "name";
            cmbCategory1.ValueMember = "id";
            cmbCategory2.DisplayMember = "name";
            cmbCategory2.ValueMember = "id";
            cmbCategory3.DisplayMember = "name";
            cmbCategory3.ValueMember = "id";
            
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
            
            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();


            string sqlSelectAll = "SELECT * FROM Products";
            
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

            
            DataSet table = new DataSet();
            MyDA.Fill(table);

            dgvProductsList.DataSource = table.Tables[0];


            dgvProductsList.Columns[0].Visible = false;
            dgvProductsList.Columns[1].Visible = false;
            dgvProductsList.Columns[2].Visible = false;
            dgvProductsList.Columns[3].Visible = false;
            dgvProductsList.Columns[1].HeaderText = "Сектор";
            dgvProductsList.Columns[2].HeaderText = "Отсек";
            dgvProductsList.Columns[3].HeaderText = "Тип";
            dgvProductsList.Columns[4].HeaderText = "Име";
            dgvProductsList.Columns[5].HeaderText = "Про.Бр.";
            dgvProductsList.Columns[6].HeaderText = "Цена";
            dgvProductsList.Columns[7].Visible = false;
            dgvProductsList.Columns[8].Visible = false;
            dgvProductsList.Columns[9].Visible = false;
            dgvProductsList.Columns[10].Visible = true;
            dgvProductsList.Columns[10].HeaderText = "Oпис";
            dgvProductsList.Columns[11].HeaderText = "Ед.мерка";
                      
        }


        private void dgvProductsList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ActiveUser.EditProducts == true)
            {
                DataGridViewRow row = dgvProductsList.CurrentRow;
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_PRODUCT_CONTROL, row);
                Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_PRODUCT_CONTROL, row);
                this.Dispose();
            }
            
        }

        private void cmbCategory1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbCategory1.Items.Count == 0) cmbCategory1.DataSource = cat.GetAllCategory1().Tables[0];
            else
            {
                
                if (cmbCategory1.SelectedValue.ToString() == "1")
                {
                    cmbCategory2.DataSource = cat.GetAllCategory2("").Tables[0];
                    cmbCategory3.DataSource = cat.GetAllCategory3("", "").Tables[0];
                }
                else
                {
                    cmbCategory2.DataSource = cat.GetAllCategory2(cmbCategory1.SelectedValue.ToString()).Tables[0];
                    cmbCategory3.DataSource = cat.GetAllCategory3(cmbCategory1.SelectedValue.ToString(), "").Tables[0];
                }
            }
            
        }

        private void cmbCategory2_SelectedValueChanged(object sender, EventArgs e)
        {

            if (cmbCategory2.Items.Count == 0) cmbCategory2.DataSource = cat.GetAllCategory2("").Tables[0];
            else
            {
                if (cmbCategory2.SelectedValue.ToString() == "1")
                {
                    cmbCategory3.DataSource = cat.GetAllCategory3("", "").Tables[0];
                }
                else
                {
                    cmbCategory3.DataSource = cat.GetAllCategory3("", cmbCategory2.SelectedValue.ToString()).Tables[0];
                }
            }

        }

        private void cmbCategory3_SelectedValueChanged(object sender, EventArgs e)
        {

            if (cmbCategory3.Items.Count == 0) cmbCategory3.DataSource = cat.GetAllCategory3("", "").Tables[0];
            if (cmbCategory2.SelectedValue.ToString() != "System.Data.DataRowView")
                if (cmbCategory3.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    table1 = prod.GetCompleateProductFromDB(cmbCategory1.SelectedValue.ToString(), cmbCategory2.SelectedValue.ToString(), cmbCategory3.SelectedValue.ToString());
                    dgvProductsList.DataSource = table1.Tables[0];
                    dgvProductsList.Update();                 
                }

        }

       

      
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (ActiveUser.EditProducts == true)
            {
                this.Dispose();
                Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_PRODUCT_CONTROL);
            }
            else
                MessageBox.Show("Немате доволно привилегии за додавање на Продукт");
        }
        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }


    }
}
